<?php
/**
 * Created by PhpStorm.
 * User: wodrow
 * Date: 17-11-10
 * Time: 上午9:08
 */

namespace console\modules\others\controllers;



use common\components\tools\City;
use common\components\tools\Model;
use common\components\tools\Tools;
use common\models\credit_old_app\SlCity;
use common\models\credit_public\SlCarModelDycw;
use common\models\credit_public\SlCarModelDycw0;
use common\models\credit_public\SlCarModelQczj;
use common\models\credit_public\SlCarSeriesDycw;
use common\models\credit_public\SlCarSeriesQczj;
use QL\QueryList;
use yii\console\Controller;
use yii\db\Exception;
use yii\httpclient\Client;

class SpiderController extends Controller
{
    /**
     * php yii others/spider/get-car-series-from-qi-che-zhi-jia
     * @throws Exception
     */
    public function actionGetCarSeriesFromQiCheZhiJia()
    {
        exit;
        $r = array_merge($this->_get_from_url_by_type('https://www.autohome.com.cn/car/0_0-0.0_0.0-0-0-0-1-0-0-0-0/', '国产车'), $this->_get_from_url_by_type('https://www.autohome.com.cn/car/0_0-0.0_0.0-0-0-0-3-0-0-0-0/', '进口车'));
        if (count($r)>SlCarSeriesQczj::find()->count()){
            $trans = \Yii::$app->db_public->beginTransaction();
            try{
                SlCarSeriesQczj::deleteAll();
                \Yii::$app->db_public->createCommand()->batchInsert(SlCarSeriesQczj::tableName(), [
                    'cs_car_type', 'cs_car_brand', 'cs_car_text', 'cs_car_price1', 'cs_car_price2', 'cs_car_url',
                ], $r)->execute();
                $trans->commit();
                echo 1;
            }catch (Exception $e){
                $trans->rollBack();
                throw $e;
            }
        }
        SlCarModelQczj::deleteAll();
        $this->actionGetCarModelFromQiCheZhiJia();
    }

    /**
     * php yii others/spider/get-car-model-from-qi-che-zhi-jia
     * @throws
     */
    public function actionGetCarModelFromQiCheZhiJia()
    {
        exit;
        /**
         * @var SlCarSeriesQczj[] $car_series
         */
        $car_series = SlCarSeriesQczj::find()->all();
//        \Yii::$app->cache->delete('GetCarModelFromQiCheZhiJia');
        $z = \Yii::$app->cache->get('GetCarModelFromQiCheZhiJia');
        if (!$z){
            $z = [];
            foreach ($car_series as $k => $v){
                $client = new Client();
                $response = $client->createRequest()
                    ->setUrl($v->cs_car_url)
                    ->setMethod('get')
                    ->send();
                $x = mb_convert_encoding($response->content, 'utf-8', 'gb2312');
                $x = str_replace('gb2312', 'utf-8', $x);
                $html = \phpQuery::newDocumentHTML($x);
                $c = $html->find('.series-list')->find('.series-content dl')->count();
                for ($i=0; $i<$c; $i++){
                    $dl = $html->find('.series-list')->find(".series-content dl:eq({$i})");
                    $c1 = $dl->find('dd')->count();
                    for ($i1=0; $i1<$c1; $i1++){
                        $dd = $dl->find("dd:eq({$i1})");
                        $model_id = $dd->find(".spec-lowest>.lowest-price")->attr("data-specid");
                        $p1 = $dd->find(".spec-name>.name-param>p:eq(0)");
//                    $model_id = $p1->attr('data-gcjid');
//                    var_dump($model_id);
                        $model_name = $p1->find("a")->html();
                        $p2 = $dd->find(".spec-guidance>p.guidance-price");
                        $price = $p2->find("span")->html();
                        $price = (double)$price;
                        $url = "http:".$p2->find("a")->attr("href");
                        if (!$model_id || !$model_name){
                            var_dump($dd->html());exit;
                        }
                        $t = [
                            'model_id' => $model_id,
                            'series_id'=>$v->cs_id,
                            'model_name' => $model_name,
                            'price' => $price,
                            'liter' => null,
                            'gear_type' => null,
                            'model_year' => null,
                            'discharge_standard' => null,
                            'seat_number' => null,
                            'min_reg_year' => null,
                            'max_reg_year' => null,
                            'url' => $url,
                        ];
                        $z[] = $t;
                        var_dump($t['model_id']." - ".$t['series_id']." - ".$t['model_name']);
                    }
                }
            }
            \Yii::$app->cache->set('GetCarModelFromQiCheZhiJia', $z, 3600*24*5);
        }
//        var_dump(count($z));exit;
        $car_model = new SlCarModelQczj();
        foreach ($z as $k => $v){
            if (!SlCarModelQczj::findOne(['model_id' => $v['model_id']])){
                $cm = clone $car_model;
                $cm->qczj_model_id = $v['model_id'];
                $cm->series_id = $v['series_id'];
                $cm->model_name = $v['model_name'];
                $cm->price = $v['price'];
                $cm->liter = $v['liter'];
                $cm->gear_type = $v['gear_type'];
                $cm->model_year = $v['model_year'];
                $cm->discharge_standard = $v['discharge_standard'];
                $cm->seat_number = $v['seat_number'];
                $cm->min_reg_year = $v['min_reg_year'];
                $cm->max_reg_year = $v['max_reg_year'];
                $cm->url = $v['url'];
                if ($cm->save()){
                    var_dump($cm->toArray());
                }else{
                    var_dump(Model::getModelError($cm));exit;
                }
            }
        }
        /*SlCarModelQczj::deleteAll();
        $trans = \Yii::$app->db_public->beginTransaction();
        try{
            \Yii::$app->db_public->createCommand()->batchInsert(SlCarModelQczj::tableName(), [
                'model_id', 'series_id', 'model_name', 'price', 'liter', 'gear_type', 'model_year', 'discharge_standard', 'seat_number', 'min_reg_year', 'max_reg_year','url',], $z)->execute();
            $trans->commit();
            echo 1;
        }catch (Exception $e){
            $trans->rollBack();
            throw $e;
        }*/
    }

    /**
     * for action: actionGetCarCategoryFromQiCheZhiJia()
     * @param $url
     * @param $type
     * @return array
     */
    private function _get_from_url_by_type($url, $type){
        $client = new Client();
        $response = $client->createRequest()
            ->setUrl($url)
            ->setMethod('get')
            ->send();
//            $x = iconv('gb2312', 'utf-8//IGNORE', $response->content);
        $x = mb_convert_encoding($response->content, 'utf-8', 'gb2312');
        $x = str_replace('gb2312', 'utf-8', $x);
        $html = \phpQuery::newDocumentHTML($x);
        $c = $html->find('#tab-content')->find('.uibox dl')->count();
        $z = [];
        for ($i=0; $i<$c; $i++){
            $dl = $html->find('#tab-content')->find(".uibox dl:eq({$i})");
            $brand = $dl->find('dt>div>a')->html();
            $dd = $dl->find('dd');
            $_c = $dd->find(".h3-tit")->count();
            for ($j = 0; $j < $_c; $j++){
                $h3 = $dd->find(".h3-tit:eq({$j})")->find("a");
                $ul = $dd->find(".rank-list-ul:eq({$j})");
                $h3 = $h3->html();
                $__c = $ul->find('li')->count();
                for ($k=0;$k<$__c;$k++){
                    $li = $ul->find("li:eq({$k})");
                    if ($li->html()){
                        $model = $li->find("h4>a")->html();
                        $url = "https:".$li->find("h4>a")->attr('href');
                        $price = $li->find("a.red")->html();
                        $p1 = $p2 = 0.00;
                        if ($price){
                            $price = str_replace('万', '', $price);
                            $t = Tools::str2arr($price, '-');
                            $p1 = $t[0];
                            $p2 = $t[1];
                        }
                        $t = [
                            'type' => $type,
                            'brand'=>$brand,
                            'model' => $h3."-".$model,
                            'price1' => $p1,
                            'price2' => $p2,
                            'url' => $url,
                        ];
                        $z[] = $t;
                    }
                }
            }
        }
        return $z;
    }

    /**
     * 从国家统计局获取地区编码
     * php yii others/spider/get-city-by-republic-of-china
     * @throws Exception
     */
    public function actionGetCityByRepublicOfChina()
    {
        $url = 'http://www.stats.gov.cn/tjsj/tjbz/xzqhdm/201703/t20170310_1471429.html';
//        $url = 'http://localhost/test2.php';
        $client = new Client();
        $response = $client->createRequest()
            ->setUrl($url)
            ->setMethod('get')
            ->send();
        $content = $response->content;
        $html = \phpQuery::newDocumentHTML($content);
        $data_div = $html->find('.TRS_PreAppend');
        $_c = $data_div->find('.MsoNormal')->count();
//        $_c = 3;
        $_p = null; // html:p
        $data = [];
        $p = null; // province
        $c = null; // city
        $q = null; //
        for ($i = 0; $i < $_c; $i++){
            $_p = $data_div->find(".MsoNormal:eq({$i})");
            if ($_p->find('b')->count()>0){
                $code = substr($_p->find('b:eq(0)')->find('span:eq(0)')->html(), 0, 6);
                $p = $_p->find('b:eq(1)')->find('span')->html();
                $data[$i]['code'] = $code;
                $data[$i]['p'] = $p;
                $data[$i]['display'] = 3;
            }else{
                $code = substr($_p->find('span:eq(1)')->html(), 0, 6);
                $data[$i]['code'] = $code;
                $__c = strlen($_p->find('span:eq(0)')->html());
                if ($__c == 3){
                    $data[$i]['p'] = $p;
                    $data[$i]['display'] = 2;
                    $c = Tools::trimall($_p->find('span:eq(3)')->html());
                    $data[$i]['c'] = $c;
                }else{
                    $data[$i]['p'] = $p;
                    $data[$i]['display'] = 1;
                    $data[$i]['c'] = $c;
                    $q = Tools::trimall($_p->find('span:eq(3)')->html());
                    $data[$i]['q'] = $q;
                }
            }
            var_dump($i);
        }
        $input_arr = [];
        foreach ($data as $k => $v){
            $arr = [];
            $area_info = "";
            $arr['state'] = $v['p'];
            $area_info = $area_info.$v['p'];
            if (isset($v['c'])){
                $area_info = $area_info." ".$v['c'];
                if (isset($v['q'])){
                    $area_info = $area_info." ".$v['q'];
                    $arr['city'] = $v['q'];
                }else{
                    $arr['city'] = $v['c'];
                }
            }else{
                $arr['city'] = $v['p'];
            }
            $arr['sz_code'] = $v['code'];
            $arr['Rome'] = City::transNameToPinYin($arr['city']);
            $arr['zm_code'] = City::newGetZipCodeByArea($area_info);
            $arr['display'] = $v['display'];
            $arr['c_order'] = 0;
            $input_arr[] = $arr;
        }
        $trans = \Yii::$app->db_old_app->beginTransaction();
        try{
            SlCity::deleteAll();
            \Yii::$app->db_old_app->createCommand()->batchInsert('sl_city', [
                'state', 'city', 'sz_code', 'Rome', 'zm_code', 'display', 'c_order'
            ], $input_arr)->execute();
            $trans->commit();
        }catch (Exception $e){
            $trans->rollBack();
            throw $e;
        }
        exit;
    }

    /**
     * php yii others/spider/dycw
     * https://www.iautos.cn/sell/
     */
    public function actionDycw($id = null)
    {
        $url = "https://www.iautos.cn/sell/";
        $html = QueryList::getInstance()->get($url)->find('.choose-boxes>.choose-boxes-wrap>.choose-brand')->html();
        $li = QueryList::getInstance()->html($html)->rules([
            'id' => ['.brand-lists-wrap>li', 'id'],
            'name' => ['.brand-lists-wrap>li>span.n', 'text'],
        ])->queryData();
        $_li = [];
        foreach ($li as $k => $v) {
            if ($v['id'] == $id){
                $_li[] = $v;
            }
            if (!$v['id']){
                unset($li[$k]);
            }
        }
        if (count($_li)){
            $li = $_li;
        }
        $data = [];
        $client = new \GuzzleHttp\Client([
            'base_uri' => "https://www.iautos.cn",
        ]);
        $model = new SlCarModelDycw0();
        foreach ($li as $k => $v){
//            if ($v['name'] != '奥迪')continue;
            $c = clone $client;
            $resp = $c->get('/car/ajax-series/', [
                'query' => [
                    'brand_id' => $v['id'],
                ],
            ]);
            $x = $v;
            if ($resp->getStatusCode() != 200){
                var_dump($resp->getBody()->getContents());
                exit;
            }
            $data1 = json_decode($resp->getBody()->getContents(), true);
            foreach ($data1 as $k1 => $v1) {
                $x['car_mfrs'] = $v1['car_mfrs'];
                foreach ($v1['car_series'] as $k2 => $v2) {
                    $x['car_series'] = $v2;
                    $c = clone $client;
                    $resp = $c->get('/car/ajax-years/', [
                        'query' => [
                            'series_id' => $v2['id'],
                        ],
                    ]);
                    if ($resp->getStatusCode() != 200){
                        var_dump($resp->getBody()->getContents());
                        exit;
                    }
                    $data2 = json_decode($resp->getBody()->getContents(), true);
                    foreach ($data2 as $k3 => $v3) {
                        $c = clone $client;
                        $resp = $c->get('/car/ajax-trims/', [
                            'query' => [
                                'series_id' => $v2['id'],
                                'year_id' => $v3['year'],
                            ],
                        ]);
                        if ($resp->getStatusCode() != 200){
                            var_dump($resp->getBody()->getContents());
                            exit;
                        }
                        $data3 = json_decode($resp->getBody()->getContents(), true);
                        if (isset($data3['displacement'])){
                            $x['displacement'] = $data3['displacement'];
                        }
                        if (isset($data3['bsfs'])){
                            $x['bsfs'] = $data3['bsfs'];
                        }
                        if (isset($data3['year_list'])){
                            $x['year_list'] = $data3['year_list'];
                        }
                        if (!isset($data3['model'])){
                            var_dump($data3);
                            var_dump($x);
                            continue;
                        }
                        foreach ($data3['model'] as $k4 => $v4) {
                            $x['model_year'] = $k4;
                            foreach ($v4 as $k5 => $v5){
                                $x['model'] = $v5;
                                $cs_car_brand = $x['id'];
                                $cs_car_text = $cs_car_brand . "-" . $x['car_series']['name_show'];
                                $cs = SlCarSeriesDycw::findOne(['cs_car_brand' => $cs_car_brand, 'cs_car_text' => $cs_car_text]);
                                if (!$cs){
                                    $cs = new SlCarSeriesDycw();
                                    $cs->cs_car_brand = $cs_car_brand;
                                    $cs->cs_car_text = $cs_car_text;
                                    if (!$cs->save()){
                                        var_dump(Model::getModelError($cs));
                                        exit;
                                    }
                                    var_dump("cs:".$cs->cs_id);
                                }
                                $cm = SlCarModelDycw::find()->where(['series_id' => $cs->cs_id, ])->orWhere(['model_id' => $x['model']['id']])->one();
                                if (!$cm){
                                    $cm = new SlCarModelDycw();
                                    $cm->series_id = $cs->cs_id;
                                    $cm->dycw_model_id = $x['model']['id'];
                                    $cm->model_name = $x['model']['title_l'];
                                    $cm->liter = $x['model']['displacement'];
                                    $cm->model_year = (string)$x['model_year'];
                                    if (preg_match('/国/', $cm->model_name, $ms)){
                                        $cm->discharge_standard = "国产";

                                    }
                                    if (preg_match('/进口/', $cm->model_name, $ms)){
                                        $cm->discharge_standard = "进口";
                                    }
                                    if (!$cm->save()){
                                        var_dump(Model::getModelError($cm));
                                        exit;
                                    }
                                    var_dump("cm:".$cm->model_id);
                                }
//                                $data[] = $x;
                                $m = SlCarModelDycw0::findOne(['model_model_id' => $x['model']['model_id']]);
                                if (!$m){
                                    $m = clone $model;
                                    $m->model_model_id = $x['model']['model_id'];
                                    $m->created_at = time();
                                }
                                $m->brand_id = $x['id'];
                                $m->brand_name = $x['name'];
                                $m->car_mfrs_id = $x['car_mfrs']['id'];
                                $m->car_mfrs_iautos_name = $x['car_mfrs']['iautos_name'];
                                $m->car_series_id = $x['car_series']['id'];
                                $m->car_series_mfrs_id = $x['car_series']['mfrs_id'];
                                $m->car_series_name = $x['car_series']['name'];
                                $m->car_series_name_show = $x['car_series']['name_show'];
                                $m->car_series_fullname = $x['car_series']['fullname'];
                                $m->car_series_pinyin = $x['car_series']['pinyin'];
                                $m->car_series_pinyin_old = $x['car_series']['pinyin_old'];
                                if (isset($data3['displacement'])){
                                    $m->displacement = json_encode($x['displacement'], JSON_UNESCAPED_UNICODE);
                                }
                                if (isset($x['bsfs'])){
                                    $m->bsfs = json_encode($x['bsfs'], JSON_UNESCAPED_UNICODE);
                                }
                                if (isset($data3['year_list'])){
                                    $m->year_list = json_encode($x['year_list'], JSON_UNESCAPED_UNICODE);
                                }
                                $m->model_year = (string)$x['model_year'];
                                $m->model_id = $x['model']['id'];
                                $m->model_name = $x['model']['name'];
                                $m->model_transmission_id = $x['model']['transmission_id'];
                                $m->model_transmission_name = $x['model']['transmission_name'];
                                $m->model_displacement = $x['model']['displacement'];
                                $m->model_new_price = $x['model']['new_price'];
                                $m->model_title = $x['model']['title'];
                                $m->model_emissions_name = $x['model']['emissions_name'];
                                $m->model_is_turbo = $x['model']['is_turbo'];
                                $m->model_show_title = $x['model']['show_title'];
                                $m->model_brand_name = $x['model']['brand_name'];
                                $m->model_series_name = $x['model']['series_name'];
                                $m->model_title_l = $x['model']['title_l'];
                                $m->updated_at = time();
                                $m->status = SlCarModelDycw0::STATUS_DEFAULT;
                                if (!$m->save()){
                                    var_dump(Model::getModelError($m));
                                    exit;
                                }
                                var_dump($m->id);
                            }
                        }
                    }
                }
            }
        }
//        var_dump($data);
    }
}